﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace ClassForWinForm
{
    public partial class Form17 : Form
    {
        public static Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        private static byte[] result = new byte[1024];
        public Form17()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            IPAddress ip = IPAddress.Parse(textBox_IP.Text.Trim());
            int port = int.Parse(textBox_Port.Text.Trim());//端口
            try
            {
                clientSocket.Connect(new IPEndPoint(ip, port)); //配置服务器IP与端口  
                textBox_Msg.Text += "连接服务器成功！" + "\r\n";
                Thread mesManageTherad = new Thread(ReceiveMessageFormSever);
                mesManageTherad.Start();
                mesManageTherad.IsBackground = true;
            }
            catch(Exception ex)
            {
                textBox_Msg.Text += "连接服务器失败！" + "\r\n";
                textBox_Msg.Text += ex.Message + "\r\n";
                return;
            }
           
        }
        private void ReceiveMessageFormSever()
        {
            while (true)
            {
                if (clientSocket.Connected)
                {
                    byte[] recvBytes = new byte[1024];
                    int length = clientSocket.Receive(recvBytes);
                    string recvStr = Encoding.UTF8.GetString(recvBytes, 0, length);
                    textBox_Msg.Text += string.Format("{0}\r\n", recvStr);
                    //ps:不要在单独的线程里面操作unity组件
                }
                else
                {
                    break;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string sendMessage = textBox_SendDta.Text.Trim();//发送到服务端的内容
                clientSocket.Send(Encoding.UTF8.GetBytes(sendMessage));//向服务器发送数据，需要发送中文则需要使用Encoding.UTF8.GetBytes()，否则会乱码
                textBox_Msg.Text += "向服务器发送消息：" + sendMessage + "\r\n";

                //接受从服务器返回的信息
                //string recvStr = "";
                //byte[] recvBytes = new byte[1024];
                //int bytes;
                //bytes = clientSocket.Receive(recvBytes, recvBytes.Length, 0);    //从服务器端接受返回信息 
                //recvStr += Encoding.UTF8.GetString(recvBytes, 0, bytes);
                ////textBox_Msg.Text += string.Format("服务端发来消息：{0}\r\n", recvStr);    //回显服务器的返回信息
                //textBox_Msg.Text += string.Format("{0}\r\n", recvStr);    //回显服务器的返回信息
                ////关闭连接并释放资源
                ////clientSocket.Close();
            }
            catch(Exception ex)
            {
                textBox_Msg.Text += ex.Message + "\r\n";
            }
        }
        
    }
}
